﻿#include "userpicker.h"
#include "ui_userpicker.h"

UserPicker::UserPicker(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UserPicker)
{
    ui->setupUi(this);

    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);

    this->isShowPlatformUser = true;

    connect(ui->treeView, &UserManageTreeView::queryUserRelations,
            [=]()
    {
        userRelationService.queryUserRelation(
                    ui->tableView,
                    ui->treeView,
                    this->isShowPlatformUser,
                    ui->lineEditQueryOverall->text(),
                    ui->lineEditQueryContact->text(),
                    ui->lineEditQueryNickName->text(),
                    ui->lineEditQueryTel->text());
        ui->tableView->hideColumn(1);
        ui->tableView->hideColumn(3);
    });

    connect(ui->tableView, &QTableView::doubleClicked,
            [=](const QModelIndex index)
    {
        Q_UNUSED(index);
        this->close();
        emit selectedFinished(ui->tableView->currentUserRelation());
    });

    connect(&userRelationService, &UserRelationService::updateUserRelationDataSuccess,
            [=](QList<UserRelation*> relations)
    {
        ui->tableView->clear();

        ui->tableView->insertUserRelation(relations, this->isShowPlatformUser);

        ui->tableView->hideColumn(1);
        ui->tableView->hideColumn(3);
    });
}

UserPicker::~UserPicker()
{
    delete ui;
}

void UserPicker::setWidgetType(UserRelation::RelationType widgetType)
{
    this->widgetType = widgetType;
    ui->tableView->setWidgetType(widgetType);
    ui->tableView->loadUserRelations(this->isShowPlatformUser);
    if(widgetType == UserRelation::Client){
        setWindowTitle("选择客户");
        ui->labelTypeName->setText("选择客户");
        ui->treeView->setCategoryType(Company::ClientCategory);
        ui->treeView->initWithCategory(Company::getClientCategory(), "客户类别", "所有客户");
    }else{
        setWindowTitle("选择供应商");
        ui->labelTypeName->setText("选择供应商");
        ui->treeView->setCategoryType(Company::GiverCategory);
        ui->treeView->initWithCategory(Company::getGiverCategory(), "供应商类别", "所有供应商");
    }
}

void UserPicker::setIsShowPlatformUser(bool isShowPlatformUser){
    this->isShowPlatformUser = isShowPlatformUser;
}

void UserPicker::loadData()
{
    ui->tableView->clear();
    ui->tableView->loadUserRelations(this->isShowPlatformUser);
    ui->tableView->hideColumn(1);
    ui->tableView->hideColumn(3);
    if(widgetType == UserRelation::Client){
        ui->treeView->initWithCategory(Company::getClientCategory(), "客户类别", "所有客户");
    }else{
        ui->treeView->initWithCategory(Company::getGiverCategory(), "供应商类别", "所有供应商");
    }
}

void UserPicker::on_lineEditQueryOverall_textChanged(const QString &text)
{
    userRelationService.queryUserRelation(
                ui->tableView,
                ui->treeView,
                this->isShowPlatformUser,
                text,
                ui->lineEditQueryNickName->text(),
                ui->lineEditQueryContact->text(),
                ui->lineEditQueryTel->text());
    ui->tableView->hideColumn(1);
    ui->tableView->hideColumn(3);
}

void UserPicker::on_lineEditQueryContact_textChanged(const QString &text)
{
    userRelationService.queryUserRelation(
                ui->tableView,
                ui->treeView,
                this->isShowPlatformUser,
                ui->lineEditQueryOverall->text(),
                ui->lineEditQueryNickName->text(),
                text,
                ui->lineEditQueryTel->text());
    ui->tableView->hideColumn(1);
    ui->tableView->hideColumn(3);
}

void UserPicker::on_lineEditQueryNickName_textChanged(const QString &text)
{
    userRelationService.queryUserRelation(
                ui->tableView,
                ui->treeView,
                this->isShowPlatformUser,
                ui->lineEditQueryOverall->text(),
                text,
                ui->lineEditQueryContact->text(),
                ui->lineEditQueryTel->text());
    ui->tableView->hideColumn(1);
    ui->tableView->hideColumn(3);
}

void UserPicker::on_lineEditQueryTel_textChanged(const QString &text)
{
    userRelationService.queryUserRelation(
                ui->tableView,
                ui->treeView,
                this->isShowPlatformUser,
                ui->lineEditQueryOverall->text(),
                ui->lineEditQueryNickName->text(),
                ui->lineEditQueryContact->text(),
                text);
    ui->tableView->hideColumn(1);
    ui->tableView->hideColumn(3);
}

void UserPicker::on_pushButtonQueryClear_clicked()
{
    ui->lineEditQueryOverall->clear();
    ui->lineEditQueryContact->clear();
    ui->lineEditQueryNickName->clear();
    ui->lineEditQueryTel->clear();
    ui->tableView->clearSelection();
}

void UserPicker::on_pushButtonUpdateData_clicked()
{
    userRelationService.updateUserRelationData(this, widgetType);
}
